<?php
/**
 * Created by xiaoZeng.
 * QQ: 1031045720 Wechat:zjwx6064
 * Date: 2018/9/16
 * Time: 22:45
 */
namespace app\api\controller;

use think\Controller;
use think\Request;
class Common extends Controller{
    public static $result_data = array(
        'code' => 0,
        'msg' => '',
        'data' => []
    );
    private static $sign_switch = 2;  //1开启签名验证，2关闭验证
    //校验token
    public function __construct(){
        parent::__construct();
        if(self::$sign_switch == 1){
            //url验证过滤 不使用签名验证
            $uri = $_SERVER['REQUEST_URI'];
            $uriarr = parse_url($uri);
            $arr = [
                '/api/order/pay', //订单支付
            ];
            if(!in_array($uriarr['path'],$arr)){
                //验证sign
                $sign = self::verification_sign(input('post.'));
                $post_sign = input('post.sign');
                if(($sign != $post_sign) || empty($post_sign)){
                    self::$result_data['msg'] = 'sign error';
                    echo json_encode(self::$result_data);
                    exit;
                }
            }
        }else{
            return;
        }
    }

    //验证sign
    protected static function verification_sign($data){
      $data['key'] = config('key');
      unset($data['sign']);
      ksort($data);
      return md5(json_encode($data,320));
    }
    /**
     * 返回json字符
     */
    public function returnJson($data){
        echo json_encode($data,JSON_UNESCAPED_UNICODE);
        exit;
    }
    /**
     *    身份证验证
     *
     *    @param    string    $id
     *    @return   boolean
     */
    function is_idcard( $id )
    {
        $id = strtoupper($id);
        $regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
        $arr_split = array();
        if(!preg_match($regx, $id))
        {
            return FALSE;
        }
        if(15==strlen($id)) //检查15位
        {
            $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";

            @preg_match($regx, $id, $arr_split);
            //检查生日日期是否正确
            $dtm_birth = "19".$arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
            if(!strtotime($dtm_birth))
            {
                return FALSE;
            } else {
                return TRUE;
            }
        }
        else           //检查18位
        {
            $regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
            @preg_match($regx, $id, $arr_split);
            $dtm_birth = $arr_split[2] . '/' . $arr_split[3]. '/' .$arr_split[4];
            if(!strtotime($dtm_birth))  //检查生日日期是否正确
            {
                return FALSE;
            }
            else
            {
                //检验18位身份证的校验码是否正确。
                //校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
                $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
                $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
                $sign = 0;
                for ( $i = 0; $i < 17; $i++ )
                {
                    $b = (int) $id{$i};
                    $w = $arr_int[$i];
                    $sign += $b * $w;
                }
                $n  = $sign % 11;
                $val_num = $arr_ch[$n];
                if ($val_num != substr($id,17, 1))
                {
                    return FALSE;
                }
                else
                {
                    return TRUE;
                }
            }
        }
    }

    /**
     *判断姓名是否全是中文
     */
    function isAllChinese($str){
        //新疆等少数民族可能有·
        if(strpos($str,'·')){
            //将·去掉，看看剩下的是不是都是中文
            $str=str_replace("·",'',$str);
            if(preg_match('/^[\x7f-\xff]+$/', $str)){
                return true;//全是中文
            }else{
                return false;//不全是中文
            }
        }else{
            if(preg_match('/^[\x7f-\xff]+$/', $str)){
                return true;//全是中文
            }else{
                return false;//不全是中文
            }
        }
    }
    /**
     * 校验手机号
     */
    function is_mobile($mobile){
        if (!preg_match("/^1[34578]\d{9}$/", $mobile)) {
            return false;
        }else{
            return true;
        }
    }

    /**
     * 获取真实IP地址
     */
    function getUserIpAddr(){
        if(!empty($_SERVER['HTTP_CLIENT_IP'])){
            //ip from share internet
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
            //ip pass from proxy
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        }else{
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }


}